package com.huonan.jb.tools.esuse.export.apps;

import cn.hutool.core.io.FileUtil;
import cn.hutool.setting.dialect.Props;
import com.huonan.jb.tools.esuse.config.PropConfig;
import com.huonan.jb.tools.esuse.dto.EsHitsInfo;
import com.huonan.jb.tools.esuse.export.Constants;
import com.huonan.jb.tools.esuse.export.enums.Env;
import com.huonan.jb.tools.esuse.export.enums.FileType;
import com.huonan.jb.tools.esuse.export.es.SearchEs;
import com.huonan.jb.tools.esuse.export.utils.YearlyCalendar;
import com.huonan.utils.DateHelper;
import com.huonan.utils.ExecutorUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description 统计量级
 * @Author lxp
 * @Date 2023/2/6 下午2:31
 * @Version V1.0
 **/
@Slf4j
public class AppEsStatic {

    static String argFile = Constants.SQL_FILE;
    /*线程数=导数并行度（指定时间切分几分）*/
    //导出数据格式。默认json
    static FileType fileType = FileType.JSON;
    static Env env = Env.PROD;

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        /*线程数=导数并行度（指定时间切分几分）*/
        int THREAD_SIZE = PropConfig.getThreadSize(argFile);
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        Props props = new Props(argFile);
        LinkedList<String> daysInYear = YearlyCalendar.getDaysInYear(2023);
        LinkedList<String> test = new LinkedList<>();
        test.add("20230101");
        test.add("20230102");
        test.add("20230103");
        //TODO
        for (String tmpDay : daysInYear) {
            String index = String.format("ik_sl_v2_%s_video", tmpDay);
            props.setProperty("index", index);
            threadSearch(props, threadPool, tmpDay);
        }
        ExecutorUtil.closePool(threadPool);
    }


    private static void threadSearch(Props props, ExecutorService threadPool, String tmpDay) {
        String file = "/Users/lxp/IdeaProjects/Study/MainTest/data/static_photo/test_result.txt";
//        CompletableFuture.runAsync(() -> {
        DateHelper.MonitorUtil.start();
        int searchCount = 0;
        String scrollId;
        searchCount++;
        try {
            EsHitsInfo dataResult = SearchEs.preHandler(env, false, props);
            if (dataResult != null && dataResult.getHistTotal() > 0) {
                FileUtil.appendUtf8String(tmpDay + "," + dataResult.getHistTotal() + FileUtil.getLineSeparator(), file);
                log.info("{} 第:{}次,数据总量:{},本次导出数据量:{}", Thread.currentThread().getName(), searchCount,
                        dataResult.getHistTotal(), dataResult.getHistTotal());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return;
        }
//        }, threadPool);
    }
}